package cate.game.role.res.guild.po;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDGuild;
import cate.game.role.Role;
import cate.game.role.RoleSnapshot;
import cate.game.role.res.guild.GuildFuncBase;
import cate.game.role.res.guild.po.my.MyGuildPerformancePO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import cate.game.role.farm.PlayTimes;
import easy.java.practice.errorcode.EcResult;

public class GuildPerformancePO extends GuildFuncBase {
    @NoteField(value = "主角的唯一ID")
    public String uid;

    @NoteField(value = "担任职位", detail = "默认设为成员")
    public byte position;

    @JsonIgnore
    @NoteField(value = "周活跃")
    public long activeWeek;

    @JsonIgnore
    @NoteField(value = "今日活跃")
    public long activeToday;

    @NoteField(value = "累计活跃")
    public long activeTotal;

    @NoteField(value = "上次捐献时间")
    public long lastDonateTime;

    @NoteField(value = "上次贡献活跃时间")
    public long lastActiveTime;

    @NoteField(value = "上次捐献公会Id")
    public String lastDonateId;

    @NoteField(value = "今日de挑战次数记录")
    public PlayTimes playTimes;

    @NoteField(value = "今日获得贡献总量")
    public long todayPValue;

    @NoteField(value = "累计贡献总值")
    public long totalPValue;

    @NoteField(value = "日常任务完成量")
    public long dayTaskCount;

    public GuildPerformancePO() {
    }

    public GuildPerformancePO(RoleSnapshot snap) {
        this.position = GDGuild.position.PRESIDENT;
        this.uid = snap.uid;
    }

    @Override
    public void initialize(GuildPO guild) {
        super.initialize(guild);
        if (playTimes == null) {
            playTimes = new PlayTimes();
        }
        playTimes.initialize();
        if (this.lastDonateId == null) {
            this.lastDonateId = guild.uid;
        }
    }

    /**
     * 跨天更新公会表现数据
     */
    public void onDaySpan() {
        activeToday = 0;
        playTimes.reset();
        dayTaskCount = 0;
        todayPValue = 0;
        doSync(false);
    }

    public void onWeekSpan() {
        activeWeek = 0;
        playTimes.reset();
        doSync(false);
    }

    /**
     * 变跟职务
     */
    public EcResult changePos(byte newPos) {
        EcResult r = new EcResult();
        if (position == newPos) {
            r.fail("职务没有变更");
            return r;
        }
        if (!GDGuild.position.check(newPos)) {
            r.fail("职务选择有误");
            return r;
        }
        position = newPos;

        getFuncDependent().guild.game.role.activeForOnlineRole(uid, e -> {
            e.getRes().guild.performance.position = newPos;
            e.getRes().guild.performance.noticeUpdate();
        });
        return r.success();
    }

    /**
     * 把公会表现数据同步到role身上
     */
    private void doSync(boolean notice) {
        Role role = getFuncDependent().guild.game.role.getRoleFromActive(this.uid);
        if (role == null || !role.getStatus().isOnline()) {
            return;
        }
        role.getRes().guild.performance.read(this);
        if (notice) {
            role.getRes().guild.performance.noticeUpdate();
        }
    }

    /**
     * 从role身上读取公会表现数据
     */
    public void read(Role role) {
        MyGuildPerformancePO performance = role.getRes().guild.performance;
        this.uid = performance.getRole().getUid();
        this.activeToday = performance.activeToday;
        this.activeWeek = performance.activeWeek;
        this.activeTotal = performance.activeTotal;
        this.lastDonateTime = performance.lastDonateTime;
        this.lastDonateId = role.getRes().guild.donate.lastDonateId;
        this.dayTaskCount = performance.dayTaskCount;
        this.lastActiveTime = performance.lastActiveTime;
        this.todayPValue = performance.todayPValue;
        this.totalPValue = performance.totalPValue;
    }

    @Override
    public int getFuncId() {
        return GDFunc.GuildFunc.MEMBER;
    }
}
